﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Chrysalis;
using System.Diagnostics;
using System.Windows.Navigation;
using Chrysalis.Services;
using ChrysalisTest.State;
using Chrysalis.Composition;

namespace ChrysalisTest.ViewModels
{
    public class CalculatorViewModel : ViewModelBase, IHasSessionState
    {
        // *** Fields ***

        private int numberX = 10;
        private int numberY = 5;

        // *** Constructors ***

        public CalculatorViewModel()
        {
            if (ChrysalisService.Current != null)
                ChrysalisService.Current.SatisfyImports(this);
        }

        // *** Properties ***

        public string Operator
        {
            get
            {
                return SessionState.Operator;
            }
            private set
            {
                SessionState.Operator = value;
                OnPropertyChanged("Operator");
                OnPropertyChanged("Total");
            }
        }

        public int NumberX
        {
            get
            {
                return numberX;
            }
            set
            {
                numberX = value;
                OnPropertyChanged("NumberX");
                OnPropertyChanged("Total");
            }
        }

        public int NumberY
        {
            get
            {
                return numberY;
            }
            set
            {
                numberY = value;
                OnPropertyChanged("NumberY");
                OnPropertyChanged("Total");
            }
        }

        [ChrysalisImport]
        public CalculatorSessionState SessionState
        {
            get;
            set;
        }

        public int Total
        {
            get
            {
                switch (Operator)
                {
                    case "+":
                        return NumberX + NumberY;
                    case "-":
                        return NumberX - NumberY;
                    case "*":
                        return NumberX * NumberY;
                    case "/":
                        return NumberX / NumberY;
                    default:
                        return 0;
                }
            }
        }

        // *** Methods ***

        public void CycleOperator()
        {
            switch (Operator)
            {
                case "+":
                    Operator = "-";
                    break;
                case "-":
                    Operator = "*";
                    break;
                case "*":
                    Operator = "/";
                    break;
                case "/":
                    Operator = "+";
                    break;
                default:
                    Operator = "+";
                    break;
            }
        }

        // *** IHasSessionState Methods ***

        void IHasSessionState.Initialize(object state)
        {
            CalculatorViewModelState vmState = (CalculatorViewModelState)state;

            if (vmState != null)
            {
                NumberX = vmState.NumberX;
                NumberY = vmState.NumberY;
            }
        }

        object IHasSessionState.SaveState()
        {
            return new CalculatorViewModelState()
            {
                NumberX = numberX,
                NumberY = numberY
            };
        }

        // *** Sub-classes ***

        public class CalculatorViewModelState
        {
            public int NumberX { get; set; }
            public int NumberY { get; set; }
        }
    }
}
